<?php
class ActivitiesController extends AppController
{
    public $name = 'Activities';
    public function index()
    {
        $this->pageTitle = __l('Activities');
        $conditions = array(
            'Activity.action !=' => 'delete'
        );
		if (!empty($this->request->params['named']['type'])) {
            switch ($this->request->params['named']['type']) {
            case 'user':
                $conditions['Activity.user_id'] = $this->request->params['named']['info'];
                break;

            case 'follow':
                $project_follower_ids = array();
                $project_follower_ids = $this->Activity->Project->ProjectFollower->find('list', array(
                    'conditions' => array(
                        'ProjectFollower.user_id' => $this->Auth->User('id')
                    ) ,
                    'fields' => array(
                        'ProjectFollower.project_id'
                    ) ,
                    'recursive' => - 1
                ));
                $conditions['Activity.project_id'] = $project_follower_ids;
                $this->pageTitle.= __l('Following Project Activity');
                break;

            case 'backed':
                $project_fund_ids = array();
                $project_fund_ids = $this->Activity->Project->ProjectFund->find('list', array(
                    'conditions' => array(
                        'ProjectFund.user_id' => $this->Auth->User('id') ,
                        
                        'ProjectFund.is_refunded' => 0,
                        'ProjectFund.is_cancelled' => 0,
                    ) ,
                    'fields' => array(
                        'ProjectFund.project_id'
                    ) ,
                    'recursive' => - 1
                ));
                $conditions['Activity.project_id'] = $project_fund_ids;
                $this->pageTitle = __l('Backed Project Activity');
                break;

            case 'myproject':
                $project_ids = array();
                $project_ids = $this->Activity->Project->find('list', array(
                    'conditions' => array(
                        'Project.user_id' => $this->Auth->User('id')
                    ) ,
                    'fields' => array(
                        'Project.id'
                    ) ,
                    'recursive' => - 1
                ));
                $conditions['Activity.project_id'] = $project_ids;
                $this->pageTitle = __l('My Project Activity');
                break;

            case 'project':
                $conditions['Activity.project_id'] = $this->request->params['named']['id'];
                break;
            }
        }
        $user_default_image = $this->Activity->Project->Attachment->find('all', array(
            'conditions' => array(
                'foreign_id' => 0,
                'class' => array(
                    'UserAvatar',
                    'Project'
                ) ,
            ) ,
        ));
        $conditions['Project.admin_suspend'] = 0;
    if (Configure::read('site.is_enable_geo_location')) {
		if (!empty($this->request->params['named']['city']) && $this->request->params['named']['city']!='all' ) {
            $cityConditions = array();
            $cityConditions['City.slug'] = $this->request->params['named']['city'];
            $find_city = $this->Activity->Project->City->find('first', array(
                'conditions' => $cityConditions,
                'fields' => array(
                    'City.id',
                    'City.slug',
                    'City.name',
                    'Country.name',
                ) ,
                'recursive' => 0
            ));
            $conditions['Project.city_id'] = $find_city['City']['id'];
	    }
	}
        $conditions['Project.project_status_id !='] = ConstProjectStatus::Pending;
        $conditions['Activity.user_id !='] = 0;
		if (!Configure::read('Project.is_enable_idea')) {
			$conditions['Activity.model !='] = 'ProjectRating';
		}
        $this->paginate = array(
            'conditions' => $conditions,
            'contain' => array(
                'User' => array(
                    'UserAvatar',
                    'fields' => array(
                        'User.id',
                        'User.username',
                        'User.email',
                    )
                ) ,
                'Project' => array(
                    'ProjectCategory',
                    'Attachment',
                    'fields' => array(
                        'Project.id',
                        'Project.name',
                        'Project.slug',
                        'Project.short_description',
                        'Project.project_category_id',
                        'Project.collected_amount',
                        'Project.project_status_id',
                        'Project.needed_amount',
                        'Project.collected_percentage',
                        'Project.project_rating_count',
                        'Project.total_ratings',
                    ),
    		   'Country' => array(
                    'fields' => array(
                        'Country.name',
                        'Country.iso2'
                    )
                ) ,
				'State' => array(
                    'fields' => array(
                        'State.name',
                    )
                ) ,
                'City' => array(
                    'fields' => array(
                        'City.name',
                        'City.slug'
                    )
                ) ,
                ) ,
                'Blog' => array(
                    'Project',
					'conditions' => array(
		                'Blog.admin_suspend' => '0',
		            )
					
                ) ,
                'ProjectFund' => array(
                    'Project'
                ) ,
                'ProjectComment' => array(
                    'Project',
		            'conditions' => array(
		                'ProjectComment.admin_suspend' => '0',
		            )
                ) ,
                'ProjectRating' => array(
                    'Project'
                ) ,
                'SecondProject' => array(
                    'fields' => array(
                        'SecondProject.id',
                        'SecondProject.name',
                        'SecondProject.slug'
                    )
                )
            ) ,
            'fields' => array(
                'Activity.id',
                'Activity.created',
                'Activity.title',
                'Activity.description',
                'Activity.model',
                'Activity.model_id',
                'Activity.action',
                'Activity.user_id',
                'Activity.project_id',
                'Activity.change'
            ) ,
            'order' => array(
                'Activity.created' => 'desc'
            ) ,
            'recursive' => 2,
            'limit' => 20
        );
        $activities = $this->paginate();
        $special_modules = array(
            'ProjectComment' => array(
                'verb' => __l('commented on a project') ,
                'type' => 'comment',
            ) ,
            'ProjectFollower' => array(
                'verb' => __l('followed on a project') ,
				'type' => 'display_field',
            ) ,
            'BlogComment' => array(
                'verb' => __l('commented on a project update') ,
                'type' => 'display_field',
            ) ,
            'ProjectFund' => array(
                'verb' => __l('has backed') ,
                'type' => 'amount',
            ) ,
            'ProjectRating' => array(
                'verb' => __l('has rated') ,
                'type' => 'display_field',
            ) ,
            'Blog' => array(
                'add' => array(
                    'verb' => __l('posted project update') ,
                ) ,
                'edit' => array(
                    'verb' => __l('updated project update') ,
                ) ,
                'title' => 'title',
                'type' => 'content',
            ) ,
            'Project' => array(
                'add' => array(
                    'verb' => __l('launched a project') ,
                ) ,
                'edit' => array(
                    'verb' => __l('updated a project') ,
                ) ,
                'title' => 'title',
                'type' => 'description',
            ) ,
        );
        $activity_infos = array();
        foreach($activities as $activity) {
            if ($activity['Activity']['model'] != 'Project') {
                $image_options = array(
                    'dimension' => 'medium_big_thumb',
                    'class' => '',
                    'alt' => $activity['Project']['name'],
                    'title' => $activity['Project']['name'],
                    'type' => 'jpg'
                );
                if (empty($activity['Project']['Attachment'])) {
                    $activity['Project']['Attachment'] = !empty($user_default_image[1]['Attachment']) ? $user_default_image[1]['Attachment'] : '';
                }
                $project_image_url = Router::url('/', true) . $this->Activity->Project->getImageUrl('Project', $activity['Project']['Attachment'], $image_options);
                $image_options = array(
                    'dimension' => 'small_thumb',
                    'class' => '',
                    'alt' => $activity['User']['username'],
                    'title' => $activity['User']['username'],
                    'type' => 'jpg'
                );
                if (empty($activity['User']['UserAvatar'])) {
                    $activity['User']['UserAvatar'] = !empty($user_default_image[0]['Attachment']) ? $user_default_image[0]['Attachment'] : '';
                }
                $user_image_url = Router::url('/', true) . $this->Activity->Project->getImageUrl('UserAvatar', $activity['User']['UserAvatar'], $image_options);
                if ($activity['Activity']['model'] == 'Blog') {
                    $url = Router::url(array(
                        'controller' => 'blogs',
                        'action' => 'view',
                        $activity['Blog']['slug']
                    ));
                    $title_url = '<a href="' . $url . '">' . $activity['Blog']['title'] . '</a>';
                    $special_modules[$activity['Activity']['model']]['verb'] = !empty($special_modules[$activity['Activity']['model']][$activity['Activity']['action']]['verb']) ? $special_modules[$activity['Activity']['model']][$activity['Activity']['action']]['verb'] : '';
                } elseif ($activity['Activity']['model'] == 'BlogComment') {
					$slug = !empty($activity['BlogComment']['Blog']['slug'])?$activity['BlogComment']['Blog']['slug']:(!empty($activity['Blog']['slug'])?$activity['Blog']['slug']:'');

                    $url = Router::url(array(
                        'controller' => 'blogs',
                        'action' => 'view',
						$slug
                    ));
					$tit =!empty($activity['BlogComment']['Blog']['title'])?$activity['BlogComment']['Blog']['title']:(!empty($activity['Blog']['title'])?$activity['Blog']['title']:'');
                    $title_url = '<a href="' . $url . '">' . $tit . '</a>';
                } elseif (($activity['Activity']['model'] == 'ProjectComment' || $activity['Activity']['model'] == 'ProjectFund')) {
                    $url = Router::url(array(
                        'controller' => 'projects',
                        'action' => 'view',
                        $activity['Project']['slug']
                    ));
                    $title_url = '<a href="' . $url . '">' . $activity['Project']['name'] . '</a>';
                }
				if($activity['Activity']['model'] == 'ProjectFollower')
				{
					$title_url = $activity['Activity']['model'];
				} 
				else {
                    $title_url = $activity['Activity']['title'];
                }
                if (!empty($activity['Project']['ProjectCategory']['name'])) {
                    $category_url = Router::url(array(
                        'controller' => 'projects',
                        'action' => 'index',
                        'category' => $activity['Project']['ProjectCategory']['slug'],
                    ));
                }
				if($activity['Activity']['model'] == 'ProjectFollower')
				{
					$content = "";
				}
				else
				{
				if(!empty($activity[$activity['Activity']['model']][$special_modules[$activity['Activity']['model']]['type']]))
				{
					$content = $activity[$activity['Activity']['model']][$special_modules[$activity['Activity']['model']]['type']];
				}
				else
				{
				    $content = $activity['Activity']['description'];
				}
				}
                $activity_infos[] = array(
                    'project_info' => array(
                        'id' => $activity['Project']['id'],
                        'name' => $activity['Project']['name'],
                        'url' => Router::url(array(
                            'controller' => 'projects',
                            'action' => 'view',
                            $activity['Project']['slug'],
                            'admin' => false
                        ) , true) ,
                        'image_url' => $project_image_url,
                        'category_url' => !empty($activity['Project']['ProjectCategory']['name']) ? $category_url : '',
                        'category' => !empty($activity['Project']['ProjectCategory']['name']) ? $activity['Project']['ProjectCategory']['name'] : '',
                        'description' => $activity['Project']['short_description'],
                        'collected_percentage' => $activity['Project']['collected_percentage'],
                        'collected_amount' => $activity['Project']['collected_amount'],
                        'needed_amount' => $activity['Project']['needed_amount'],
                        'project_status_id' => $activity['Project']['project_status_id'],
                        'project_rating_count' => $activity['Project']['project_rating_count'],
                        'total_ratings' => $activity['Project']['total_ratings'],
					    'city_name' => !empty($activity['Project']['City']['name']) ? $activity['Project']['City']['name'] :'',
					    'city_slug' => !empty($activity['Project']['City']['slug']) ? $activity['Project']['City']['slug'] :'',
					    'state_name' => !empty($activity['Project']['State']['name']) ? $activity['Project']['State']['name'] :'',
    		    		'country_name' => !empty($activity['Project']['Country']['name']) ? $activity['Project']['Country']['name']:'',
					    'country_iso2' => !empty($activity['Project']['Country']['iso2']) ? $activity['Project']['Country']['iso2']:'',
                    ) ,
                    'activity_info' => array(
                        'model' => $activity['Activity']['model'],
                        'username' => $activity['User']['username'],
                        'user_url' => Router::url(array(
                            'controller' => 'users',
                            'action' => 'view',
                            $activity['User']['username'],
                            'admin' => false
                        ) , true) ,
                        'user_image_url' => $user_image_url,
                        'type' => !empty($special_modules[$activity['Activity']['model']]['type']) ? $special_modules[$activity['Activity']['model']]['type'] : '',
                        'action' => !empty($special_modules[$activity['Activity']['model']]['verb']) ? $special_modules[$activity['Activity']['model']]['verb'] : '',
						'action_type' => $activity['Activity']['action'],
                        'time' => $activity['Activity']['created'],
                        'title' => $title_url,
                        'content' => !empty($activity[$activity['Activity']['model']][$special_modules[$activity['Activity']['model']]['type']]) ? $activity[$activity['Activity']['model']][$special_modules[$activity['Activity']['model']]['type']] : $activity['Activity']['description'],
                        'blog_comments' => !empty($activity[$activity['Activity']['model']]['BlogComment']) ? $activity[$activity['Activity']['model']]['BlogComment'] : '',
                    )
                );

            } else {
                $image_options = array(
                    'dimension' => 'medium_big_thumb',
                    'class' => '',
                    'alt' => $activity[$activity['Activity']['model']]['name'],
                    'title' => $activity[$activity['Activity']['model']]['name'],
                    'type' => 'jpg'
                );
                if (empty($activity[$activity['Activity']['model']]['Attachment'])) {
                    $activity[$activity['Activity']['model']]['Attachment'] = !empty($user_default_image[1]['Attachment']) ? $user_default_image[1]['Attachment'] : '';
                }
                $project_image_url = Router::url('/', true) . $this->Activity->Project->getImageUrl('Project', $activity[$activity['Activity']['model']]['Attachment'], $image_options);
                $image_options = array(
                    'dimension' => 'small_thumb',
                    'class' => '',
                    'alt' => $activity['User']['username'],
                    'title' => $activity['User']['username'],
                    'type' => 'jpg'
                );
                if (empty($activity['User']['UserAvatar'])) {
                    $activity['User']['UserAvatar'] = !empty($user_default_image[0]['Attachment']) ? $user_default_image[0]['Attachment'] : '';
                }
                $user_image_url = Router::url('/', true) . $this->Activity->Project->getImageUrl('UserAvatar', $activity['User']['UserAvatar'], $image_options);
                if (!empty($activity['Project']['ProjectCategory']['name'])) {
                    $category_url = Router::url(array(
                        'controller' => 'projects',
                        'action' => 'index',
                        'category' => $activity['Project']['ProjectCategory']['slug'],
                    ));
                }
                $activity_infos[] = array(
                    'project_info' => array(
                        'id' => $activity[$activity['Activity']['model']]['id'],
                        'name' => $activity[$activity['Activity']['model']]['name'],
                        'url' => Router::url(array(
                            'controller' => 'projects',
                            'action' => 'view',
                            $activity[$activity['Activity']['model']]['slug'],
                            'admin' => false
                        ) , true) ,
                        'image_url' => $project_image_url,
                        'category_url' => !empty($activity['Project']['ProjectCategory']['name']) ? $category_url : '',
                        'category' => $activity[$activity['Activity']['model']]['ProjectCategory']['name'],
                        'collected_percentage' => $activity[$activity['Activity']['model']]['collected_percentage'],
                        'collected_amount' => $activity[$activity['Activity']['model']]['collected_amount'],
                        'needed_amount' => $activity[$activity['Activity']['model']]['needed_amount'],
                        'project_status_id' => $activity[$activity['Activity']['model']]['project_status_id'],
                        'project_rating_count' => $activity[$activity['Activity']['model']]['project_rating_count'],
                        'total_ratings' => $activity[$activity['Activity']['model']]['total_ratings'],
		    			'city_name' => !empty($activity['Project']['City']['name']) ? $activity['Project']['City']['name'] :'',
					    'city_slug' => !empty($activity['Project']['City']['slug']) ? $activity['Project']['City']['slug'] :'',
					    'state_name' => !empty($activity['Project']['State']['name']) ? $activity['Project']['State']['name'] :'',
    		    		'country_name' => !empty($activity['Project']['Country']['name']) ? $activity['Project']['Country']['name']:'',
    					'country_iso2' => !empty($activity['Project']['Country']['iso2']) ? $activity['Project']['Country']['iso2']:'',
	    		    ) ,
                    'activity_info' => array(
                        'username' => $activity['User']['username'],
                        'user_url' => Router::url(array(
                            'controller' => 'users',
                            'action' => 'view',
                            $activity['User']['username'],
                            'admin' => false
                        ) , true) ,
                        'user_image_url' => $user_image_url,
                        'model' => $activity['Activity']['model'],
                        'type' => !empty($special_modules[$activity['Activity']['model']]['type']) ? $special_modules[$activity['Activity']['model']]['type'] : '',
                        'action' => !empty($special_modules[$activity['Activity']['model']][$activity['Activity']['action']]['verb']) ? $special_modules[$activity['Activity']['model']][$activity['Activity']['action']]['verb'] : '',
						'action_type' => $activity['Activity']['action'],
                        'time' => $activity['Activity']['created'],
                        'title' => !empty($activity[$activity['Activity']['model']]['title']) ? $activity[$activity['Activity']['model']]['title'] : '',
                        'content' => $activity[$activity['Activity']['model']]['short_description'],
                        'blog_comments' => isset($activity[$activity['Activity']['model']]['BlogComment']) ? $activity[$activity['Activity']['model']]['BlogComment'] : '',
                    )
                );
            }
							

        }
        $this->set('activities', $activity_infos);
		if(!empty($this->request->params['url']['ext']) && $this->request->params['url']['ext'] =='rss'){
				$this->autoRender = false;
				$this->render('index');
		}elseif (!empty($this->request->params['named']['type']) && !empty($this->request->params['named']['id']) && $this->request->params['named']['type'] == 'project') {
			$this->autoRender = false;
			$this->render('project_activities');
		}
    }
}
?>